﻿using Furion;
using Furion.Schedule;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TimeJob.Application;
using TimeJob.Core;

namespace TimeJob.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddConsoleFormatter();
        services.AddDatabaseLogging<DatabaseLoggingWriter>(options => { });
        services.AddJwt<JwtHandler>();

        services.AddCorsAccessor();

        services.AddControllers()
                .AddInjectWithUnifyResult();

        services.AddSingleton<MyJob>();

        services.AddSchedule(options =>
        {
            // 注册作业，并配置作业触发器
            // options.AddJob<MyJob>("myjob"); // 表示每秒执行
            //options.AddPersistence<DbJobPersistence>();
        });


        DbContext.Instance.CodeFirst.InitTables<LogTable>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        

        //app.UseHttpsRedirection();

        app.UseRouting();

        app.UseStaticFiles();
        app.UseScheduleUI();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
